package com.amarsoft.apot.controller;

import com.amarsoft.apot.config.MailConfig;
import com.amarsoft.apot.entity.User;
import com.amarsoft.apot.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/hello")
public class HelloController {

    private static Map<String,Object> paramMap = new HashMap<>();

    private static int sum = 0;

    @Autowired
    private UserService userService;


    @Autowired
    private MailConfig mailConfig;


    @Resource(name="taskCompareExecutor")
    private ThreadPoolExecutor taskMigrationExecutor;

    @GetMapping("/hello1")
    public String test01(Map paramMap) {

        System.out.println("进入test01了");
        AtomicInteger value = new AtomicInteger(0);
        try {
            for(int i =0 ;i<= 9;i++) {
                taskMigrationExecutor.execute(() -> {

                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    value.getAndIncrement();
                    sum++;
                    System.out.println(Thread.currentThread().getName()+"完成了"+sum);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"结束了"+value.get());
        return "hello world1"+mailConfig.getHost();
    }

    @GetMapping("/hello2")
    public String test02() {
        System.out.println(sum);
        return "hello world2";
    }

    @GetMapping("/getUser")
    public User getUser(){
        User user = new User();
        user.setUsername("xxx");
        user.setPassword("123456");
        return user;
    }

    @ApiOperation("xxx的接口")
    @GetMapping("/szh")
    public String kuang(@ApiParam("用户信息")User user){
        String temp = user.getUsername();
        return "hello"+temp;
    }

    @ApiOperation("xxx的接口")
    @PostMapping("/szh2")
    public String kuang2(String username){
        String temp = username;
        return "hello"+temp;
    }
}
